#coding: utf-8
import json
import requests

def ipapi(ip):
	url = 'http://api.map.baidu.com/location/ip?ak=paIUXAsVq0Nsyj1Qn04GxUPfcxZNG9V9&ip=' + ip
	headers = {'User-Agent':'Mozilla/5.0 (Windows; NT 10.0; win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.163 Safari/537.36'}
	#User-Agent:Mozilla/5.0(Macintosh;IntelMacOSX10_7_0)AppleWebKit/535.11(KHTML,likeGecko)Chrome/17.0.963.56Safari/535.11
	#Mozilla/5.0(Macintosh;IntelMacOSX10.6;rv:2.0.1)Gecko/20100101Firefox/4.0.1

	r = requests.get(url, headers=headers, timeout=6)
	jsdic = json.loads(r.content)

	# if jsdic.has_key('content'):

	if 'content' in jsdic:

		c = jsdic['content']['address']
		pointx = jsdic['content']['point']['x']
		pointy = jsdic['content']['point']['y']
		return c, pointx, pointy
	else:#default
		return '太谷', '112.55246', '37.42161'

def getmap(x, y):
	im = 'http://api.map.baidu.com/staticimage/v2?ak=paIUXAsVq0Nsyj1Qn04GxUPfcxZNG9V9&mcode=666666&center=' + x + ',' + y + '&width=500&height=300&zoom=11'
	return im
'''if __name__ == '__main__':
	ipapi(ip)'''
